package com.neusoft.neuiotms.dcam.workflow.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuFinanceSLDTO;
import com.neusoft.neuiotms.dcam.workflow.model.RukuFinanceAssetSL;
import com.neusoft.neuiotms.dcam.workflow.model.RukuFinanceConfirmSL;
import com.neusoft.neuiotms.dcam.workflow.model.RukuFinanceSL;
import com.neusoft.neuiotms.dcam.workflow.repository.RukuFinanceAssetSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.RukuFinanceSLRepository;

@Component
public class RukuFinanceSLConverter extends AbstractConverter<RukuFinanceSL, RukuFinanceSLDTO> {

    @Autowired
    private RukuFinanceSLRepository rukuSLRepository;
    @Autowired
    private RukuFinanceAssetSLConverter rukuAssetSLConverter;
    @Autowired
    private RukuFinanceAssetSLRepository rukuAssetSLRepository;

    @Override
    public RukuFinanceSL toModel(final RukuFinanceSLDTO dto) {
        final RukuFinanceSL model;
        if (dto.isNew()) {
            model = new RukuFinanceSL();
        } else {
            model = this.rukuSLRepository.getOne(dto.getId());

            final List<RukuFinanceAssetSL> assetList = model.getAssetList().stream().collect(Collectors.toList());
            assetList.forEach(k -> {
                model.getAssetList().remove(k);
                this.rukuAssetSLRepository.delete(k);
            });
        }

        dto.getAssetList().stream().map(k -> {
            final RukuFinanceAssetSL assetModel = this.rukuAssetSLConverter.toModel(k);
            assetModel.setRuku(model);
            return assetModel;
        }).filter(s -> null != s).forEach(model.getAssetList()::add);

        model.setTitle(dto.getTitle());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setTaskId(dto.getTaskId());
        model.setReceiveDate(dto.getReceiveDate());

        return model;
    }

    @Override
    public RukuFinanceSLDTO toDTO(final RukuFinanceSL model, final boolean forListView) {
        final RukuFinanceSLDTO dto = new RukuFinanceSLDTO();

        dto.setId(model.getId());
        dto.setReceiveDate(model.getReceiveDate());
        dto.setCreateTime(model.getCreateTime());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setTaskId(model.getTaskId());
        dto.setTitle(model.getTitle());
        if (!forListView) {

            if (null != model.getAssetList()) {
                model.getAssetList().stream().map(l -> this.rukuAssetSLConverter.toDTO(l))
                        .forEach(dto.getAssetList()::add);
            }
        }
        return dto;
    }

    public RukuFinanceConfirmSL copy(final RukuFinanceSL model) {
        final RukuFinanceConfirmSL dto = new RukuFinanceConfirmSL();
        dto.setReceiveDate(model.getReceiveDate());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setTitle(model.getTitle());
        return dto;
    }
}
